//
//  LZTVisitorView.swift
//  LZTWeibo
//
//  Created by Yin jianxun on 16/8/10.
//  Copyright © 2016年 YinJianxun. All rights reserved.
//

import UIKit
import SnapKit

class LZTVisitorView: UIView {
    
    //声明闭包：用于visitorView告知cisitorVc，注册登录 按钮被点击了，需要跳转了
    var closure: (() -> ())?
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //按钮的监听方法
    //@objc 标识该方法是类似于OC中消息循环调用该方法，由于在Swift中，静态语言的缘故，按钮在触发点击事件后，对应的方法没有被事先加载，会报错。
    //利用 该标识-@objc 可以让private方法利用OC方式进行调用
    @objc private func buttonClick() {
        
        //visitorView的闭包 执行
        closure?()
        
    }
    
    //依据不同界面，进行展示不同的控件
    //首页 消息 发现 我 各自有不同的visitor界面
    func setupVisitorViewInfo(imgName: String?,title: String?) {
        
        //如果 传入的imgName或者title为nil，证明是首页，如果不是，就是其他界面
        if let img = imgName, let tit = title {
            //非首页
            iconImageView.image = UIImage(named: img)
            messageLabel.text = tit
            
            //设置圆圈的显示与否
            circleImageView.hidden = true
            
        }else {
            // 首页
            setupAnim()
        }
        
    }
    
    //设置圆圈的动画
    func setupAnim(){
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = M_PI * 2
        anim.duration = 10
        anim.repeatCount = MAXFLOAT
        
        // 如果用户切换子控制器或者退到后台 默认动画移除
        anim.removedOnCompletion = false
        // 添加动画
        circleImageView.layer.addAnimation(anim, forKey: nil)
    }
    
    //设置UI
    private func setupUI() {
        
        backgroundColor = RGB(237, green: 237, blue: 237)
        
        //添加 控件 后 添加约束
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        circleImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
            
        }
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        messageLabel.snp_makeConstraints { (make) in
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.height.equalTo(36)
            make.centerX.equalTo(self)
        }
        registerButton.snp_makeConstraints { (make) in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        loginButton.snp_makeConstraints { (make) in
            make.size.top.equalTo(registerButton)
            make.trailing.equalTo(messageLabel)
        }
        
    }
    //懒加载控件 圆圈试图
    private lazy var circleImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_image_smallicon")
    
    //遮挡
    private lazy var maskImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_mask_smallicon")
    
    //房子
    private lazy var iconImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_image_house")
    
    //文字
    private lazy var messageLabel: UILabel = {
        
        let label = UILabel()
        label.textColor = AppThemeColor
        label.font = UIFont.systemFontOfSize(15)
        label.numberOfLines = 0
        label.textAlignment = .Center
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        //最大现实宽度
        label.preferredMaxLayoutWidth = 240
        
        return label
        
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
        
        let btn = UIButton()
        //添加 点击事件
        btn.addTarget(self, action: #selector(LZTVisitorView.buttonClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(AppThemeColor, forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        return btn
        
    }()
    //登录按钮
    private lazy var loginButton: UIButton = {
        
        let btn = UIButton()
        //添加 点击事件
        btn.addTarget(self, action: #selector(LZTVisitorView.buttonClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(AppThemeColor, forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        return btn
        
    }()
    
}






